import os
import csv
import pandas as pd
# 这个是取唯一值

def get_all_csv(source_path, dest_path):
    res = []
    for fileName in os.listdir(source_path):
        source_file_path = source_path+os.sep+fileName
        if fileName.endswith(".csv"):
            res.append(
                (source_file_path, os.path.splitext(dest_path+os.sep +
                                                    os.path.split(source_file_path)[-1])[0]
                 )
            )
        elif os.path.isdir(source_file_path):
            res.extend(get_all_csv(source_path+os.sep +
                       fileName, dest_path+os.sep+fileName))
    return res


files = get_all_csv("csv", "")
for file, tag in files:
    print("solving", file)
    df = pd.read_csv(file).groupby(
        ["BASETYPE", "SUBTYPE", "CATEGORY"]).size().to_frame("COUNT").reset_index()
    df.insert(0, 'tag', [tag]*df.shape[0])
    # print(df)
    df.to_csv("res.csv", mode='a', encoding='utf-8',
              header=False, index=False)
    # break
# print(df)
